﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace G.APITest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        // 在 Frame 导航之后，请确保选中表示当前页的 HyperlinkButton
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
            radMenu.Items.Clear();
            Page page = e.Content as Page;
            var menuItems = G.SL.Controls.PageMenuService.GetMenu(page);
            if (menuItems.Count > 0)
            {
                foreach (var item in menuItems)
                {
                    Telerik.Windows.Controls.RadMenuItem radmenuitem = new Telerik.Windows.Controls.RadMenuItem();
                    AddMenus(item, radmenuitem);
                    if (item.HasSplitter)
                        radMenu.Items.Add(new Telerik.Windows.Controls.RadMenuItem() { IsSeparator = true });
                    radMenu.Items.Add(radmenuitem);
                }
            }
            menuItems.CollectionChanged += (s, _e) =>
            {
                if (_e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                {
                    foreach (SL.Controls.MenuItem item in _e.NewItems)
                    {
                        Telerik.Windows.Controls.RadMenuItem radmenuitem = new Telerik.Windows.Controls.RadMenuItem();
                        AddMenus(item, radmenuitem);
                        if (item.HasSplitter)
                            radMenu.Items.Add(new Telerik.Windows.Controls.RadMenuItem() { IsSeparator = true });
                        radMenu.Items.Add(radmenuitem);
                    }
                }
                else if (_e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
                {

                }
            };
        }

        private void AddMenus(G.SL.Controls.MenuItem item, Telerik.Windows.Controls.RadMenuItem radmenuitem)
        {
            radmenuitem.Header = item.Name;
            item.Items.CollectionChanged += (s, e) =>
            {
                if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                {
                    foreach (SL.Controls.MenuItem i in e.NewItems)
                    {
                        Telerik.Windows.Controls.RadMenuItem radmenu = new Telerik.Windows.Controls.RadMenuItem();
                        AddMenus(i, radmenu);
                        if (i.HasSplitter)
                            radmenuitem.Items.Add(new Telerik.Windows.Controls.RadMenuItem() { IsSeparator = true });
                        radmenuitem.Items.Add(radmenu);
                    }
                }
            };

            if (item.Items.Count > 0)
            {
                foreach (var i in item.Items)
                {
                    Telerik.Windows.Controls.RadMenuItem radmenu = new Telerik.Windows.Controls.RadMenuItem();
                    AddMenus(i, radmenu);
                    if (i.HasSplitter)
                        radmenuitem.Items.Add(new Telerik.Windows.Controls.RadMenuItem() { IsSeparator = true });
                    radmenuitem.Items.Add(radmenu);
                }
            }
            else
            {
                radmenuitem.Command = item.Command;
                radmenuitem.CommandParameter = item.CommandParamter;
            }

        }


        // 如果导航过程中出现错误，则显示错误窗口
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

        private void ContentFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.ContentFrame.CanGoBack)
                this.ContentFrame.GoBack();
        }
    }
}